/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Collections;
import com.cashmate.hibernate.pojo.Deposits;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Dine
 */
public class DepositsDAO extends DAO {

    public DepositsDAO() {
        super();
    }

    @SuppressWarnings("unchecked")
    public List<Deposits> getAll() throws Exception {
        List<Deposits> a = (List<Deposits>) getSession().createQuery("from Deposits a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Deposits get(int id) throws Exception {
        Deposits a = (Deposits) getSession().createQuery("from Deposits a where a.depositid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Deposits a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Deposits a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Deposits> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Deposits> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }
    
// USE insert(Deposit a) above...
//    @SuppressWarnings("unchecked")
//    public void addDeposit(Deposits deposit) {
//        try {
//
//            session.saveOrUpdate(deposit);
//            transaction.commit();
//            session.flush();
//
//        } catch (Exception e) {
//            System.out.println("TRANSACTIONdep: " + e.toString());
//            transaction.rollback();
//        }
//    }

    @SuppressWarnings("unchecked")
    public List<Deposits> getByCollectionID(Collections id) throws Exception{
        List<Deposits> d = (List<Deposits>) getSession().createQuery("from Deposits d where d.collections=:id").setParameter("id", id).setCacheable(true).list();
        flush();
        return d;
    }
}
